'use strict';
import React, { useEffect, useContext } from 'react'
import { connect } from "react-redux";
import { Navigate } from "react-router-dom";
import HttpContext from "../../server/httpContext";
import { getUserInfo } from '../store/user'


const Index = (props) => {
  let context = useContext(HttpContext);
  console.log(context, "context-user");
  useEffect(() => {
    if (!("name" in props.userinfo)) {
      // 客户端获取数据
      props.getUserInfo()
    }
  }, [])

  // 在这里做逻辑判断然后重定向
  // if (true) {
  //   console.log(context);
  //   context.staticContext.action = "REPLACE";
  //   context.staticContext.url = "/";
  //   // TODO v6 删除了Redirect 使用Navigate重定向
  //   return <Navigate to="/" />
  // }

  return <div>
    <h1>你好{props.userinfo.name},你们最棒的人是{props.userinfo.best}</h1>
  </div>
};

Index.loadData = (store) => {
  return store.dispatch(getUserInfo())
}

export default connect(state => ({
  userinfo: state.user.userinfo
}),
  { getUserInfo }
)(Index)